import axios from 'axios'

const api = axios.create({
  baseURL: 'http://localhost:3000'
})

export const login = (username, password) => {
  return api.get(`/users?username=${username}&password=${password}`)
}

export const register = (user) => {
  return api.post('/users', user)
}

export const getCategories = () => {
  return api.get('/categories')
}

export const getDrugs = (params = {}) => {
  let url = '/drugs'
  if (params.category) {
    url += `?category=${params.category}`
  }
  return api.get(url)
}

export const addToCart = (userId, drug) => {
  return api.post('/cart', {
    userId,
    drug,
    quantity: 1
  })
}

export const getOrders = (userId) => {
  return api.get(`/orders?userId=${userId}`)
}

export const createOrder = (order) => {
  return api.post('/orders', order)
}

export const getAnnouncements = () => {
  return api.get('/announcements')
}

export const updateOrder = (orderId, orderData) => {
  return api.put(`/orders/${orderId}`, orderData)
}

export const updateUser = (userId, userData) => {
  return api.put(`/users/${userId}`, userData)
}

export const createCategory = (category) => {
  return api.post('/categories', category)
}

export const updateCategory = (categoryId, category) => {
  return api.put(`/categories/${categoryId}`, category)
}

export const deleteCategory = (categoryId) => {
  return api.delete(`/categories/${categoryId}`)
}

export const getCartItems = (userId) => {
  return api.get(`/cart?userId=${userId}`)
}

export const updateCartItem = (cartItemId, data) => {
  return api.put(`/cart/${cartItemId}`, data)
}

export const removeFromCart = (cartItemId) => {
  return api.delete(`/cart/${cartItemId}`)
}

export const getBanners = () => {
  return api.get('/banners')
}

export default api 